package com.sigmazero13.bgjeeves.model;

public class GameInfo implements Comparable<GameInfo> {
	public static Double DEFAULT_RATING = 7.0;
	public static Integer NO_MAX_PLAYERS = 99;
	
	
	private Integer gameId = 0;
	private String name = "N/A";
	private Integer minPlayers = 1;
	private Integer maxPlayers = NO_MAX_PLAYERS;
	private Integer bggLength = 60;
	private Double bggRating = null;
	private Boolean manualEntry = false;
	private GameInfoCustom custom = null;
	private boolean useCustom = true;
	
	
	public GameInfo() {}
	
	protected GameInfo(GameInfo toCopy) {
		this.gameId = toCopy.gameId;
		this.name = toCopy.name;
		this.minPlayers = toCopy.minPlayers;
		this.maxPlayers = toCopy.maxPlayers;
		this.bggLength = toCopy.bggLength;
		this.bggRating = toCopy.bggRating;
		this.manualEntry = toCopy.manualEntry;
	}
	
	public Integer getGameId() {
		return gameId;
	}
	public void setGameId(Integer gameId) {
		this.gameId = gameId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getMinPlayers() {
		return getMinPlayers(useCustom);
	}
	public Integer getMinPlayers(boolean useCustom) {
		Integer min = minPlayers;
		if (useCustom && custom != null && custom.getMinPlayers() != null) {
			min = custom.getMinPlayers();
		}
		return min;
	}
	public void setMinPlayers(Integer minPlayers) {
		this.minPlayers = minPlayers;
	}
	public Integer getMaxPlayers() {
		return getMaxPlayers(useCustom);
	}
	public Integer getMaxPlayers(boolean useCustom) {
		Integer max = maxPlayers;
		if (useCustom && custom != null && custom.getMaxPlayers() != null) {
			max = custom.getMaxPlayers();
		}
		return max;
	}
	public void setMaxPlayers(Integer maxPlayers) {
		this.maxPlayers = maxPlayers;
	}
	public Integer getBggLength() {
		return bggLength;
	}
	public void setBggLength(Integer bggLength) {
		this.bggLength = bggLength;
	}
	public Double getBggRating() {
		if (bggRating == null) {
			return DEFAULT_RATING;
		} else {
			return bggRating;
		}
	}
	public void setBggRating(Double bggRating) {
		this.bggRating = bggRating;
	}
	public Boolean isManualEntry() {
		return manualEntry;
	}
	public void setManualEntry(Boolean manualEntry) {
		this.manualEntry = manualEntry;
	}
	public GameInfoCustom getCustomInfo() {
		return custom;
	}
	public void setCustomInfo(GameInfoCustom custom) {
		this.custom = custom;
	}
	public boolean getUseCustom() {
		return useCustom;
	}
	public void setUseCustom(boolean useCustom) {
		this.useCustom = useCustom;
	}
	
	public GameInfo copy() {
		return new GameInfo(this);
	}
	
	public String getNumPlayersString() {
		Integer min = getMinPlayers();
		Integer max = getMaxPlayers();
		
		String base = min.toString();
		if (max >= NO_MAX_PLAYERS) {
			return base + "+";
		} else if (min == max) {
			return base;
		} else {
			return base + " - " + max.toString();
		}
	}
	
	@Override 
	public String toString() {
		return name + ", " + getNumPlayersString() + " (" + bggLength + " mins)";
	}
	
	@Override
	public int hashCode() {
		return gameId;
	}
	
	@Override
	public int compareTo(GameInfo another) {
		if (another == null) return 1;
		return another.name.compareTo(name);
	}
}
